<template>
  <el-container>
    <el-header :style="{fontsize: '12px'}">单因素方差分析</el-header>
    <el-main>
      <el-form @submit.prevent="submitData">
        <el-form-item label="选择因素列">
          <el-select v-model="selectedFactor" placeholder="请选择">
            <el-option
              v-for="column in columnOptions"
              :key="column"
              :label="column"
              :value="column"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显著性水平">
          <el-input-number
            v-model="alpha"
            :precision="2"
            :step="0.01"
            :min="0"
            :max="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitData">提交</el-button>
        </el-form-item>
      </el-form>
      <div v-if="statistics">
        <p>F统计量: {{ statistics.f_statistic }}</p>
        <p>p值: {{ statistics.p_value }}</p>
        <p>结论: {{ statistics.conclusion }}</p>
      </div>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useStore } from 'vuex';
import axios from 'axios';
import { ElMessage } from 'element-plus';

const store = useStore();
const selectedFactor = ref();
const alpha = ref(0.05);
const statistics = ref(null);
const data = computed(() => store.state.tableData);
const columnOptions = computed(() => {
  if (data.value.length > 0) {
    return Object.keys(data.value[0]);
  }
  return [];
});

const submitData = async () => {
  if (selectedFactor.value.length === 0) {
    ElMessage.error('请选择一个因素列');
    return;
  }

  try {
    const payload = {
      data: store.state.tableData,
      factors: [selectedFactor.value],
      alpha: alpha.value,
    };

    const response = await axios.post('http://101.43.68.110:8000/api/anova/', payload);
    statistics.value = response.data;
    console.log(statistics.value)
  } catch (error) {
    ElMessage.error('数据提交失败');
    console.error(error);
  }
};
</script>

<style scoped></style>